/*
 * File: look1_iu16lu16n16Ds32I_mxHw0VIW.c
 *
 * Code generated for Simulink model 'TABL'.
 *
 * Model version                  : 3.208
 * Simulink Coder version         : 9.4 (R2020b) 29-Jul-2020
 * C/C++ source code generated on : Tue Mar 29 13:56:59 2022
 */

#include "rtwtypes.h"
#include "look1_iu16lu16n16Ds32I_mxHw0VIW.h"

uint8_T look1_iu16lu16n16Ds32I_mxHw0VIW(uint16_T u0, const uint16_T bp0[], const
    uint16_T table[], uint32_T maxIndex)
{
    uint32_T bpIdx;
    uint32_T iLeft;
    uint32_T iRght;
    uint16_T frac;
    uint16_T yL_0d0;
    uint8_T y;

    /* Column-major Lookup 1-D
       Canonical function name: look1_iu16lu16n16Ds32Iu8_binlcas
       Search method: 'binary'
       Use previous index: 'off'
       Interpolation method: 'Linear point-slope'
       Extrapolation method: 'Clip'
       Use last breakpoint for index at or above upper limit: 'on'
       Remove protection against out-of-range input in generated code: 'off'
       Rounding mode: 'simplest'
     */
    /* Prelookup - Index and Fraction
       Index Search method: 'binary'
       Extrapolation method: 'Clip'
       Use previous index: 'off'
       Use last breakpoint for index at or above upper limit: 'on'
       Remove protection against out-of-range input in generated code: 'off'
       Rounding mode: 'simplest'
     */
    if (u0 <= bp0[0U])
    {
        iLeft = 0U;
        frac = 0U;
    }
    else if (u0 < bp0[maxIndex])
    {
        /* Binary Search */
        bpIdx = maxIndex >> 1U;
        iLeft = 0U;
        iRght = maxIndex;
        while (iRght - iLeft > 1U)
        {
            if (u0 < bp0[bpIdx])
            {
                iRght = bpIdx;
            }
            else
            {
                iLeft = bpIdx;
            }

            bpIdx = (iRght + iLeft) >> 1U;
        }

        frac = bp0[iLeft];
        frac = (uint16_T)(((uint32_T)(uint16_T)((uint32_T)u0 - frac) << 16) /
                          (uint16_T)((uint32_T)bp0[iLeft + 1U] - frac));
    }
    else
    {
        iLeft = maxIndex;
        frac = 0U;
    }

    /* Column-major Interpolation 1-D
       Canonical function name: intrp1d_u16s32u8s16u32u16n16la_s
       Interpolation method: 'Linear point-slope'
       Use last breakpoint for index at or above upper limit: 'on'
       Rounding mode: 'simplest'
       Overflow mode: 'wrapping'
     */
    if (iLeft == maxIndex)
    {
        y = (uint8_T)table[iLeft];
    }
    else
    {
        yL_0d0 = table[iLeft];
        y = (uint8_T)((uint32_T)(uint8_T)(((table[iLeft + 1U] - yL_0d0) * frac) >>
                       16) + (uint8_T)yL_0d0);
    }

    return y;
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
